package gentle.service.impl;

import gentle.base.SqlMapper;
import gentle.entity.CustomerInfo;
import gentle.entity.Dept;
import gentle.entity.VipLevelInfo;
import gentle.service.MemberProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class MemberProductServiceImpl implements MemberProductService {

    @Autowired
    private SqlMapper sqlMapper;

    public VipLevelInfo getVipLevel(String reservationsId,String abbreviation) {
        String sql1="select * from dept where abbreviation='"+abbreviation+"' limit 1";
        Dept dept=sqlMapper.selectOne(sql1,Dept.class);
        String vtable=dept.getAreaAbbreviation()+"vip_level_info";
        String sql = "select ifnull(sum(actual_account_balance),0) as moneyBalance from reservemoney_deal " +
                "where reservations_id = '" + reservationsId + "' and mode!='消费' ";
        CustomerInfo info=sqlMapper.selectOne(sql, CustomerInfo.class);
        String sql2 = "select * from "+ vtable+" a ";
        List<VipLevelInfo> infov = sqlMapper.selectList(sql2, VipLevelInfo.class);
        for (VipLevelInfo iv:infov) {
            if (iv.getMoneyCoefficientTwo() > info.getMoneyBalance() && info.getMoneyBalance() >= iv.getMoneyCoefficient()) {
                String sesql = "select * from "+ vtable+" a where vip_level_name='" + iv.getVipLevelName() + "' limit 1";
                VipLevelInfo vipLevelInfo = sqlMapper.selectOne(sesql, VipLevelInfo.class);
                return vipLevelInfo;
            }
        }
        String sesql = "select * from "+ vtable+" a where vip_level_name='入门会员' limit 1";
        VipLevelInfo vipLevelInfo = sqlMapper.selectOne(sesql, VipLevelInfo.class);
        return vipLevelInfo;
    }
}